<template>
  <div class="container">
    <div class="card_body" style="position: relative;">
      <div class="top">
        <div class="topLeft">
          <div class="keywords">
            <span style="font-weight: bold;">关键字</span>
            <el-input
              v-model="pageParams.keyword"
              size="mini"
              placeholder="根据文章标题搜索"
              style="width: 190px; height: 32px; margin-left: 10px;"
              @input="search"
            />
          </div>

          <div class="state">
            <span style="font-weight: bold;">状态</span>
            <el-select v-model="pageParams.state" size="mini" style="width: 190px; height: 32px; margin-left: 10px;">
              <el-option v-for="(item) in stateArr" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </div>

          <div class="btnAddSearch">
            <el-button size="mini" style="margin-left: 10px;" @click="clean">清除</el-button>
            <el-button size="mini" type="primary" @click="search">搜索</el-button>
          </div>
        </div>

        <div class="topRight">
          <div class="add">
            <el-button
              size="mini"
              type="success"
              @click="addSkill"
            ><i class="el-icon-edit" />新增技巧</el-button>
          </div>
        </div>
      </div>
      <el-alert
        style="margin-top: 15px;"
        type="info"
        :closable="false"
      >
        <i class="el-icon-info" />
        <span style="margin-left: 10px; font-size: 13px;">数据一共{{ counts }}条</span>
      </el-alert>
    </div>

    <el-table
      :data="articleList"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
      />
      <el-table-column
        label="文章标题"
        prop="title"
      >
        <template v-slot="{ row }">
          <span>{{ row.title }}</span>
          <i v-if="row.videoURL" class="el-icon-film" style="font-size: 20px; color: blue; cursor: pointer;" @click="videoPlay(row.videoURL)" />
        </template>
      </el-table-column>
      <el-table-column
        label="阅读数"
        prop="visits"
      />
      <el-table-column
        label="录入人"
        prop="username"
      />
      <el-table-column
        label="录入时间"
        prop="createTime"
      >
        <template v-slot="{ row }">
          {{ row.createTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        prop="state"
      >
        <template v-slot="{row}">
          <span>{{ row.state===1?'已启用':'未启用' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template v-slot="{row}">
          <!-- <p>{{ row }}</p> -->
          <el-button type="text" @click="preview(row.id, row.username)">预览</el-button>
          <el-button type="text" @click="operate(row.id, row.state)">
            {{ row.state===1 ? '禁用' : '启用' }}
          </el-button>
          <el-button type="text" :disabled="!!row.state" @click="editArt(row.id)">修改</el-button>
          <el-button type="text" :disabled="!!row.state" @click="removeArt(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-row type="flex" justify="end" style="height: 60px; line-height: 60px;">
      <el-pagination
        background
        layout="sizes, prev, pager, next, jumper"
        :page-sizes="[2, 5, 7, 10]"
        :total="counts"
        :page-size="pageParams.pagesize"
        :current-page.sync="pageParams.page"
        @current-change="changePage"
        @size-change="sizeChange"
      />
    </el-row>

    <!-- 显示视频 -->
    <div v-show="showVideo" class="video">
      <div class="close" @click="closeVideo">
        <i class="el-icon-close" />
      </div>

      <div class="box">
        <video ref="video" :src="videoURL" autoplay="autoplay" muted="muted" style="margin-left: 90px;" />
      </div>
    </div>

    <!-- 显示预览 -->
    <articles
      ref="articlePre"
      :show-art-pre-dialog="showArtPreDialog"
      :current-node-id="currentNodeId"
      :current-username="currentUsername"
      @closeArtPreDialog="showArtPreDialog=false"
    />

    <!-- 添加文章 -->
    <articlesAdd
      ref="articlesAdd"
      :current-node-id="currentNodeId"
      :show-art-add="showArtAdd"
      @closeArtAdd="showArtAdd=false"
      @update="getArticleList"
      @openDialog="showArtAdd=true"
    />
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import moment from 'moment'
import articles from '@/module-hmmm/components/articles-preview.vue'
import articlesAdd from '@/module-hmmm/components/articles-add.vue'
export default {
  name: 'Article',
  components: {
    articles,
    articlesAdd
  },
  filters: {
    formatDate(val) {
      return moment(val).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  data() {
    return {
      state: '', // 状态
      keywords: '', // 关键词
      articleList: [], // 文章列表
      // pageParams: {
      //   counts: null, // 数据数量
      //   page: 1, // 当前页
      //   pages: null, // 共多少页
      //   pagesize: null // 一页展示多少
      // },
      artObj: {},

      counts: null, // 数据数量
      pages: null, // 总共多少页数据

      pageParams: {
        page: 1,
        pagesize: 5,
        keyword: '',
        state: null
      },
      // 文章状态下拉框数据
      stateArr: [
        { label: '启用', value: 1, id: 1 },
        { label: '未启用', value: 0, id: 2 }
      ],
      // 视频地址
      videoURL: '',
      // 显示视频弹窗
      showVideo: false,
      // 打开文章预览
      showArtPreDialog: false,
      // 当前点击节点的id
      currentNodeId: null,
      // 当前节点的用户名
      currentUsername: '',
      // 控制新增组件显示隐藏
      showArtAdd: false

    }
  },
  created() {
    this.getArticleList()
  },
  methods: {
    // 获取文章列表
    async getArticleList() {
      // const { data: { counts, items, page, pages, pagesize } } = await list()
      // this.pageParams.counts = counts
      // this.articleList = items
      // this.pageParams.page = page
      // this.pageParams.pages = pages
      // this.pageParams.pagesize = pagesize

      const { data: { counts, items, pages }} = await list(this.pageParams)
      // this.artObj = data
      this.counts = counts // 总共多少条数据
      this.articleList = items // 文章列表
      this.pages = pages // 总共多少页
      // this.pageParams.pagesize = pagesize
    },
    // 清除搜索框和状态下拉框的内容
    clean() {
      this.pageParams.keyword = ''
      this.pageParams.state = ''
      this.getArticleList()
    },
    // 页面切换时
    changePage(newPage) {
      console.log(newPage)
      this.pageParams.page = newPage
      this.getArticleList()
    },
    // 搜索按钮功能
    search() {
      this.pageParams.page = 1
      this.getArticleList(this.pageParams)
    },
    // 当页面显示多少条改变时
    sizeChange(newSize) {
      console.log(newSize)
      this.pageParams.pagesize = newSize
      this.getArticleList(this.pageParams)
    },
    // 点击视频按钮
    videoPlay(videoURL) {
      this.videoURL = videoURL
      this.$refs.video.currentTime = 0
      this.$refs.video.play()
      this.showVideo = true
    },
    // 预览的弹窗
    preview(id, username) {
      this.currentNodeId = id
      this.currentUsername = username
      this.showArtPreDialog = true
      // console.log(this.$refs.articlePre)
      this.$nextTick(() => {
        this.$refs.articlePre.getArticleDetail()
      })
    },
    // 操作文章状态
    async operate(id, state) {
      state = state === 1 ? 0 : 1
      await changeState({ id, state })
      this.$message.success('操作成功!')
      this.getArticleList()
    },
    // 新增
    addSkill() {
      this.showArtAdd = true
    },
    // 修改
    editArt(id) {
      this.currentNodeId = id
      console.log(this.$refs.articlesAdd)
      this.$nextTick(() => {
        this.$refs.articlesAdd.getCurrentArtDetail()
      })
    },
    // 删除文章
    removeArt(id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        if (this.pageParams.page > 1 && this.articleList.length === 1) {
          this.pageParams.page--
        }
        // 调接口删除文章
        await remove({ id })
        // 更新列表
        this.getArticleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 关闭视频并停止播放
    closeVideo() {
      this.showVideo = false
      this.$refs.video.pause()
    }

  }

}
</script>

<style scoped lang='scss'>
  .card_body {
    padding: 20px;
    background-color: #fff;
  }

  .keywords  {
    display: inline-block;
  }

  .state {
    display: inline-block;
    margin-left: 40px;
  }
  .btnAddSearch {
    display: inline-block;
  }
  .add {
    display: inline-block;
  }

  .top {
    display: flex;
    justify-content: space-between;
  }

  .container {
    padding: 10px;
    border-radius: 20px;
  }

  .el-icon-info {
    font-size: 16px;
  }
  .video {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background-color: rgb(0,0,0,.3);
    overflow: hidden;
  }
  .box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    height: 50%;
    transform: translate(-67%,-50%);
    margin-top: 450px;
  }
  .el-table--border::after, .el-table--group::after, .el-table::before {
    background-color: transparent;
  }

  .close {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 80px;
    left: 50%;
    border-radius: 50%;
    background-color: rgb(0,0,0,.3);
  }
  .close i {
    font-size: 40px;
    margin-top: 6px;
    margin-left: 6px;
    color: #fff;
  }
</style>
